<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2024/8/27
 * Time: 14:00
 */

namespace app\privatearea\controller\mall\v1;

use app\common\cachemodel\privatearea\PrivateAreaMallGoods;
use app\common\cachemodel\privatearea\PrivateAreaMallGoodsAttr;
use app\common\cachemodel\privatearea\PrivateAreaMallUser;
use app\common\cachemodel\privatearea\PrivateAreaOrders;
use app\common\lib\Util;
use GuzzleHttp\Exception\GuzzleException;
use think\Controller;
use think\Log;
use think\response\Json;

class SfNotify extends Controller
{
    /**
     * 顺丰运单路由变动信息
     * @return Json
     * @throws GuzzleException
     */
    public function routeStatus()
    {
        $pram = request()->param();
        Log::write('顺丰路由变动信息：' . json_encode($pram), 'sf_kd', true);

        $pramTmp = [
            'acceptAddress' => '北京市',
            'mailno' => 'SF1690019223235',
            'orderid' => '57202408291519188631',
            'acceptTime' => '2024-08-29 14:48:17',
            'opCode' => '50',
            'remark' => '顺丰已收件',
            'id' => '989027313558945792',
        ];

        $Orders = new PrivateAreaOrders();
        try
        {
            $orderList = $Orders->where('waybill_no', $pram['mailno'])->column('uid,qid,extend,status', 'order_id');

            switch ($pram['opCode'])
            {
                case '50'://已揽件
                    foreach ($orderList as $item)
                    {
                        if ((int)$item['status'] === 6)
                        {
                            break;
                        }
                        //将快递单号，物流公司整合到订单extend字段中
                        $extend = json_decode($item['extend'], true);
                        $extend['kd']['id'] = $pram['mailno'];
                        $extend['kd']['company'] = '顺丰快递';
                        $extend['kd']['notify_remark'] = $pram['remark'];
                        $extend['kd']['send_time'] = $this->request->time();
                        $extend = json_encode($extend, JSON_UNESCAPED_UNICODE);
                        //跟新订单状态，更新订单物流信息
                        if ($Orders->updateOrderInfo($item['order_id'], ['status' => 6, 'kd_status' => $pram['opCode'], 'extend' => $extend]))
                        {
                            $tel = (new PrivateAreaMallUser())->where('uid', $item['uid'])->value('tel');
                            $goodsAttr = (new PrivateAreaMallGoodsAttr())->getInfoById($item['qid']);
                            $ganame = $goodsAttr['name'];
                            $gname = (new PrivateAreaMallGoods())->where('id', $goodsAttr['gid'])->value('name');
                            Util::sendHySms([$tel], '', 153027, ['goods_name' => $gname . '-' . $ganame, 'kd_name' => '顺丰快递', 'kd_id' => $pram['mailno']], '【黑博士的疗愈水晶】');//私域实物发货通知
                        } else
                        {
                            Log::write($item['order_id'] . '订单更新失败', 'sf_kd', true);
                            return json(['status' => 'ERR']);
                        }
                    }
                    break;
                default:
                    foreach ($orderList as $item)
                    {
                        $extend = json_decode($item['extend'], true);
                        $extend['kd']['notify_remark'] = $pram['remark'];
                        $Orders->updateOrderInfo($item['order_id'], ['kd_status' => $pram['opCode'], 'extend' => $extend]);
                    }
                    break;
            }
        }
        catch (\Exception $e)
        {
            Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return json(['status' => 'ERR']);
        }
        return json(['status' => 'OK']);
    }

    /**
     * 成功下单后，小哥上门揽收前，顺丰对订单进行调度的信息集合
     * @return Json
     * @throws GuzzleException
     */
    public function orderStatus()
    {
        $pram = request()->param();
        Log::write('成功下单后，小哥上门揽收前，顺丰对订单进行调度的信息集合。可获取到对应小哥的联系方式：' . json_encode($pram), 'sf_kd', true);
        $pramTmp = [
            'lastTime' => '',//最晚上门时间
            'orderNo' => '57202408151109355965',
            'netCode' => '',//网点
            'bookTime' => '',//客户预约时间
            'empPhone' => '',//收件员手机号
            'createTm' => '2024-08-28 16:19:18',//状态操作时间
            'empCode' => '',//收件员工工号
            'carrierCode' => 'SF',
            'orderStateDesc' => '调度失败/等待',//状态描述
            'orderStateCode' => '04',//订单状态
            'waybillNo' => 'SF1020136054571',//运单号
            'ver' => 'v1',
        ];

        switch ($pram['orderStateCode'])
        {
            case'04'://调度失败/等待
                break;
            case '04-40001'://调度成功+收派员信息
                break;
            case '04-40037'://下单已接收
                break;
            case '04-40002-40021'://电话号码异常(电话错误/空号/停机)
                break;
            case '04-40002-40022'://无法进入客户处,且客户不愿出来
                break;
            case '04-40002-40004'://暂未联系上客户(电话无人接听/无法接通/关机)
                break;
            case '04-40002-40005'://经客户同意，更改收派时间
                break;
            case '04-40002-40027'://天气原因,延误收派件
                break;
            case '04-40002-40028'://交通原因,延误收派件
                break;
            case '04-40002-40031'://不符合收件要求,无法揽收
                break;
            case '04-40002-40014'://客户取消寄件
                break;
            case '04-40002-40015'://客户重复下单
                break;
            case '04-40002-40016'://下错单需转单
                break;
            case '04-40002-40035'://工作量过大,延误收派件
                break;
            case '04-40002-40036'://重货需转单
                break;
            case '00-40045-1000'://调度取消订单
                break;
            case '00-2000'://客户已取消订单
                break;
            case '05-40003'://已正常收件状态
                break;
            case '00'://客户取消（此状态无状态描述）
                break;
        }
        $Orders = new PrivateAreaOrders();
        $orderList = $Orders->where('waybill_no', $pram['waybillNo'])->column('uid,extend', 'order_id');
        foreach ($orderList as $item)
        {
            $extend = $item['extend'];
            $extend = json_decode($extend, true);
            $extend['kd']['empCode'] = $pram['empCode'];
            $extend['kd']['empPhone'] = $pram['empPhone'];
            $extend['kd']['lastTime'] = $pram['lastTime'];
            $extend = json_encode($extend, JSON_UNESCAPED_UNICODE);
            //跟新订单状态，更新订单物流信息
            $Orders->updateOrderInfo($item['order_id'], ['extend' => $extend]);
        }
        return json(['status' => 'OK']);
    }

    /**
     * 快递员揽件后，将真实折前运费推送给客户。
     * @return \think\response\Json
     */
    public function expressFee()
    {
        $pram = request()->param();
        Log::write('快递员揽件后，将真实折前运费推送给客户：' . json_encode($pram), 'sf_kd', true);
        return json(['status' => 'OK']);//ERR
    }
}
